(function(a) {
    a.uniform = {
        options: {
            selectClass: "selector",
            radioClass: "radio",
            checkboxClass: "checker",
            fileClass: "uploader",
            filenameClass: "filename",
            fileBtnClass: "action",
            fileDefaultText: "No file selected",
            fileBtnText: "Choose File",
            checkedClass: "checked",
            focusClass: "focus",
            disabledClass: "disabled",
            buttonClass: "button",
            activeClass: "active",
            hoverClass: "hover",
            useID: true,
            idPrefix: "uniform",
            resetSelector: false,
            autoHide: true
        },
        elements: []
    };
    if (a.browser.msie && a.browser.version < 7) {
        a.support.selectOpacity = false
    } else {
        a.support.selectOpacity = true
    }
    a.fn.uniform = function(k) {
        k = a.extend(a.uniform.options, k);
        var d = this;
        if (k.resetSelector != false) {
            a(k.resetSelector).mouseup(function() {
                function l() {
                    a.uniform.update(d)
                }
                setTimeout(l, 10)
            })
        }

        function j(l) {
            $el = a(l);
            $el.addClass($el.attr("type"));
            b(l)
        }

        function g(l) {
            a(l).addClass("uniform");
            b(l)
        }

        function i(o) {
            var m = a(o);
            var p = a("<div>"),
                l = a("<span>");
            p.addClass(k.buttonClass);
            if (k.useID && m.attr("id") != "") {
                p.attr("id", k.idPrefix + "-" + m.attr("id"))
            }
            var n;
            if (m.is("a") || m.is("button")) {
                n = m.text()
            } else {
                if (m.is(":submit") || m.is(":reset") || m.is("input[type=button]")) {
                    n = m.attr("value")
                }
            }
            n = n == "" ? m.is(":reset") ? "Reset" : "Submit" : n;
            l.html(n);
            m.css("opacity", 0);
            m.wrap(p);
            m.wrap(l);
            p = m.closest("div");
            l = m.closest("span");
            if (m.is(":disabled")) {
                p.addClass(k.disabledClass)
            }
            p.bind({
                "mouseenter.uniform": function() {
                    p.addClass(k.hoverClass)
                },
                "mouseleave.uniform": function() {
                    p.removeClass(k.hoverClass);
                    p.removeClass(k.activeClass)
                },
                "mousedown.uniform touchbegin.uniform": function() {
                    p.addClass(k.activeClass)
                },
                "mouseup.uniform touchend.uniform": function() {
                    p.removeClass(k.activeClass)
                },
                "click.uniform touchend.uniform": function(r) {
                    if (a(r.target).is("span") || a(r.target).is("div")) {
                        if (o[0].dispatchEvent) {
                            var q = document.createEvent("MouseEvents");
                            q.initEvent("click", true, true);
                            o[0].dispatchEvent(q)
                        } else {
                            o[0].click()
                        }
                    }
                }
            });
            o.bind({
                "focus.uniform": function() {
                    p.addClass(k.focusClass)
                },
                "blur.uniform": function() {
                    p.removeClass(k.focusClass)
                }
            });
            a.uniform.noSelect(p);
            b(o)
        }

        function e(o) {
            var m = a(o);
            var p = a("<div />"),
                l = a("<span />");
            if (!m.css("display") == "none" && k.autoHide) {
                p.hide()
            }
            p.addClass(k.selectClass);
            if (k.useID && o.attr("id") != "") {
                p.attr("id", k.idPrefix + "-" + o.attr("id"))
            }
            var n = o.find(":selected:first");
            if (n.length == 0) {
                n = o.find("option:first")
            }
            l.html(n.html());
            o.css("opacity", 0);
            o.wrap(p);
            o.before(l);
            p = o.parent("div");
            l = o.siblings("span");
            o.bind({
                "change.uniform": function() {
                    l.text(o.find(":selected").html());
                    p.removeClass(k.activeClass)
                },
                "focus.uniform": function() {
                    p.addClass(k.focusClass)
                },
                "blur.uniform": function() {
                    p.removeClass(k.focusClass);
                    p.removeClass(k.activeClass)
                },
                "mousedown.uniform touchbegin.uniform": function() {
                    p.addClass(k.activeClass)
                },
                "mouseup.uniform touchend.uniform": function() {
                    p.removeClass(k.activeClass)
                },
                "click.uniform touchend.uniform": function() {
                    p.removeClass(k.activeClass)
                },
                "mouseenter.uniform": function() {
                    p.addClass(k.hoverClass)
                },
                "mouseleave.uniform": function() {
                    p.removeClass(k.hoverClass);
                    p.removeClass(k.activeClass)
                },
                "keyup.uniform": function() {
                    l.text(o.find(":selected").html())
                }
            });
            if (a(o).attr("disabled")) {
                p.addClass(k.disabledClass)
            }
            a.uniform.noSelect(l);
            b(o)
        }

        function f(n) {
            var m = a(n);
            var o = a("<div />"),
                l = a("<span />");
            if (!m.css("display") == "none" && k.autoHide) {
                o.hide()
            }
            o.addClass(k.checkboxClass);
            if (k.useID && n.attr("id") != "") {
                o.attr("id", k.idPrefix + "-" + n.attr("id"))
            }
            a(n).wrap(o);
            a(n).wrap(l);
            l = n.parent();
            o = l.parent();
            a(n).css("opacity", 0).bind({
                "focus.uniform": function() {
                    o.addClass(k.focusClass)
                },
                "blur.uniform": function() {
                    o.removeClass(k.focusClass)
                },
                "click.uniform touchend.uniform": function() {
                    if (!a(n).attr("checked")) {
                        l.removeClass(k.checkedClass)
                    } else {
                        l.addClass(k.checkedClass)
                    }
                },
                "mousedown.uniform touchbegin.uniform": function() {
                    o.addClass(k.activeClass)
                },
                "mouseup.uniform touchend.uniform": function() {
                    o.removeClass(k.activeClass)
                },
                "mouseenter.uniform": function() {
                    o.addClass(k.hoverClass)
                },
                "mouseleave.uniform": function() {
                    o.removeClass(k.hoverClass);
                    o.removeClass(k.activeClass)
                }
            });
            if (a(n).attr("checked")) {
                l.addClass(k.checkedClass)
            }
            if (a(n).attr("disabled")) {
                o.addClass(k.disabledClass)
            }
            b(n)
        }

        function c(n) {
            var m = a(n);
            var o = a("<div />"),
                l = a("<span />");
            if (!m.css("display") == "none" && k.autoHide) {
                o.hide()
            }
            o.addClass(k.radioClass);
            if (k.useID && n.attr("id") != "") {
                o.attr("id", k.idPrefix + "-" + n.attr("id"))
            }
            a(n).wrap(o);
            a(n).wrap(l);
            l = n.parent();
            o = l.parent();
            a(n).css("opacity", 0).bind({
                "focus.uniform": function() {
                    o.addClass(k.focusClass)
                },
                "blur.uniform": function() {
                    o.removeClass(k.focusClass)
                },
                "click.uniform touchend.uniform": function() {
                    if (!a(n).attr("checked")) {
                        l.removeClass(k.checkedClass)
                    } else {
                        var p = k.radioClass.split(" ")[0];
                        a("." + p + " span." + k.checkedClass + ":has([name='" + a(n).attr("name") + "'])").removeClass(k.checkedClass);
                        l.addClass(k.checkedClass)
                    }
                },
                "mousedown.uniform touchend.uniform": function() {
                    if (!a(n).is(":disabled")) {
                        o.addClass(k.activeClass)
                    }
                },
                "mouseup.uniform touchbegin.uniform": function() {
                    o.removeClass(k.activeClass)
                },
                "mouseenter.uniform touchend.uniform": function() {
                    o.addClass(k.hoverClass)
                },
                "mouseleave.uniform": function() {
                    o.removeClass(k.hoverClass);
                    o.removeClass(k.activeClass)
                }
            });
            if (a(n).attr("checked")) {
                l.addClass(k.checkedClass)
            }
            if (a(n).attr("disabled")) {
                o.addClass(k.disabledClass)
            }
            b(n)
        }

        function h(q) {
            var o = a(q);
            var r = a("<span />"),
                p = a("<span>" + k.fileDefaultText + "</span>"),
                m = a("<span>" + k.fileBtnText + "</span>");
            if (!o.css("display") == "none" && k.autoHide) {
                r.hide()
            }
            r.addClass(k.fileClass);
            p.addClass(k.filenameClass);
            m.addClass(k.fileBtnClass);
            if (k.useID && o.attr("id") != "") {
                r.attr("id", k.idPrefix + "-" + o.attr("id"))
            }
            o.wrap(r);
            o.after(m);
            o.after(p);
            r = o.closest("span");
            p = o.siblings("." + k.filenameClass);
            m = o.siblings("." + k.fileBtnClass);
            if (!o.attr("size")) {
                var l = r.width();
                o.attr("size", l / 10)
            }
            var n = function() {
                var s = o.val();
                if (s === "") {
                    s = k.fileDefaultText
                } else {
                    s = s.split(/[\/\\]+/);
                    s = s[(s.length - 1)]
                }
                p.text(s)
            };
            n();
            o.css("opacity", 0).bind({
                "focus.uniform": function() {
                    r.addClass(k.focusClass)
                },
                "blur.uniform": function() {
                    r.removeClass(k.focusClass)
                },
                "mousedown.uniform": function() {
                    if (!a(q).is(":disabled")) {
                        r.addClass(k.activeClass)
                    }
                },
                "mouseup.uniform": function() {
                    r.removeClass(k.activeClass)
                },
                "mouseenter.uniform": function() {
                    r.addClass(k.hoverClass)
                },
                "mouseleave.uniform": function() {
                    r.removeClass(k.hoverClass);
                    r.removeClass(k.activeClass)
                }
            });
            if (a.browser.msie) {
                o.bind("click.uniform.ie7", function() {
                    setTimeout(n, 0)
                })
            } else {
                o.bind("change.uniform", n)
            } if (o.attr("disabled")) {
                r.addClass(k.disabledClass)
            }
            a.uniform.noSelect(p);
            a.uniform.noSelect(m);
            b(q)
        }
        a.uniform.restore = function(l) {
            if (l == undefined) {
                l = a(a.uniform.elements)
            }
            a(l).each(function() {
                if (a(this).is(":checkbox")) {
                    a(this).unwrap().unwrap()
                } else {
                    if (a(this).is("select")) {
                        a(this).siblings("span").remove();
                        a(this).unwrap()
                    } else {
                        if (a(this).is(":radio")) {
                            a(this).unwrap().unwrap()
                        } else {
                            if (a(this).is(":file")) {
                                a(this).siblings("span").remove();
                                a(this).unwrap()
                            } else {
                                if (a(this).is("button, :submit, :reset, a, input[type='button']")) {
                                    a(this).unwrap().unwrap()
                                }
                            }
                        }
                    }
                }
                a(this).unbind(".uniform");
                a(this).css("opacity", "1");
                var m = a.inArray(a(l), a.uniform.elements);
                a.uniform.elements.splice(m, 1)
            })
        };

        function b(l) {
            l = a(l).get();
            if (l.length > 1) {
                a.each(l, function(m, n) {
                    a.uniform.elements.push(n)
                })
            } else {
                a.uniform.elements.push(l)
            }
        }
        a.uniform.noSelect = function(l) {
            function m() {
                return false
            }
            a(l).each(function() {
                this.onselectstart = this.ondragstart = m;
                a(this).mousedown(m).css({
                    MozUserSelect: "none"
                })
            })
        };
        a.uniform.update = function(l) {
            if (l == undefined) {
                l = a(a.uniform.elements)
            }
            l = a(l);
            l.each(function() {
                var n = a(this);
                if (n.is("select")) {
                    var m = n.siblings("span");
                    var p = n.parent("div");
                    p.removeClass(k.hoverClass + " " + k.focusClass + " " + k.activeClass);
                    m.html(n.find(":selected").html());
                    if (n.is(":disabled")) {
                        p.addClass(k.disabledClass)
                    } else {
                        p.removeClass(k.disabledClass)
                    }
                } else {
                    if (n.is(":checkbox")) {
                        var m = n.closest("span");
                        var p = n.closest("div");
                        p.removeClass(k.hoverClass + " " + k.focusClass + " " + k.activeClass);
                        m.removeClass(k.checkedClass);
                        if (n.is(":checked")) {
                            m.addClass(k.checkedClass)
                        }
                        if (n.is(":disabled")) {
                            p.addClass(k.disabledClass)
                        } else {
                            p.removeClass(k.disabledClass)
                        }
                    } else {
                        if (n.is(":radio")) {
                            var m = n.closest("span");
                            var p = n.closest("div");
                            p.removeClass(k.hoverClass + " " + k.focusClass + " " + k.activeClass);
                            m.removeClass(k.checkedClass);
                            if (n.is(":checked")) {
                                m.addClass(k.checkedClass)
                            }
                            if (n.is(":disabled")) {
                                p.addClass(k.disabledClass)
                            } else {
                                p.removeClass(k.disabledClass)
                            }
                        } else {
                            if (n.is(":file")) {
                                var p = n.parent("span");
                                var o = n.siblings(k.filenameClass);
                                btnTag = n.siblings(k.fileBtnClass);
                                p.removeClass(k.hoverClass + " " + k.focusClass + " " + k.activeClass);
                                o.text(n.val());
                                if (n.is(":disabled")) {
                                    p.addClass(k.disabledClass)
                                } else {
                                    p.removeClass(k.disabledClass)
                                }
                            } else {
                                if (n.is(":submit") || n.is(":reset") || n.is("button") || n.is("a") || l.is("input[type=button]")) {
                                    var p = n.closest("div");
                                    p.removeClass(k.hoverClass + " " + k.focusClass + " " + k.activeClass);
                                    if (n.is(":disabled")) {
                                        p.addClass(k.disabledClass)
                                    } else {
                                        p.removeClass(k.disabledClass)
                                    }
                                }
                            }
                        }
                    }
                }
            })
        };
        return this.each(function() {
            if (a.support.selectOpacity) {
                var l = a(this);
                if (l.is("select")) {
                    if (l.attr("multiple") != true) {
                        if (l.attr("size") == undefined || l.attr("size") <= 1) {
                            e(l)
                        }
                    }
                } else {
                    if (l.is(":checkbox")) {
                        f(l)
                    } else {
                        if (l.is(":radio")) {
                            c(l)
                        } else {
                            if (l.is(":file")) {
                                h(l)
                            } else {
                                if (l.is(":text, :password, input[type='email']")) {
                                    j(l)
                                } else {
                                    if (l.is("textarea")) {
                                        g(l)
                                    } else {
                                        if (l.is("a") || l.is(":submit") || l.is(":reset") || l.is("button") || l.is("input[type=button]")) {
                                            i(l)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        })
    }
})(jQuery);